package com.feng.zshop.backend.service.impl;

import com.feng.zshop.backend.dao.CustomerDao;
import com.feng.zshop.backend.param.CustomerParam;
import com.feng.zshop.backend.service.CustomerService;
import com.feng.zshop.common.constant.CustomerConstant;
import com.feng.zshop.common.exception.DaoException;
import com.feng.zshop.common.exception.ServiceException;
import com.feng.zshop.model.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Lee
 * @date 2019/2/24
 */
@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerDao customerDao;

    @Transactional
    @Override
    public void updateStatus(Integer id) throws ServiceException {
        int valid = 0;
        Customer customer = customerDao.getCustomerById(id);
        if (CustomerConstant.STATUS_ENABLE.equals(customer.getValid())){
            valid = CustomerConstant.STATUS_DISABLE;
        } else {
            valid = CustomerConstant.STATUS_ENABLE;
        }
        try {
            customerDao.updateStatus(id, valid);
        } catch (DaoException e) {
            throw new ServiceException("用户更新失败");
        }
    }

    @Override
    public List<Customer> findByParam(CustomerParam customerParam) {
        return customerDao.listByParam(customerParam);
    }

    @Override
    public Customer findById(Integer id) {
        return customerDao.getCustomerById(id);
    }

}
